package com.tianji.jersey.resource.jsonp;

import javax.servlet.http.HttpServletRequest;
import javax.ws.rs.DELETE;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.QueryParam;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.MultivaluedMap;
import javax.ws.rs.core.UriInfo;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Component;

import com.tianji.jersey.resource.AnswerVO;
import com.tianji.jersey.resource.AnswerVoList;
import com.tianji.jersey.resource.client.IntResult;

@Component

@Path("/jsonp")
public class JsonpResorce {
	private Log log = LogFactory.getLog(this.getClass());
	@Autowired
	@Qualifier("hostGroup2Rest")
	private String hostGroup2Rest;
	@Autowired
	@Qualifier("hostGroup1Rest")
	private String hostGroup1Rest;
	@Context
	UriInfo uriInfo;
	@Context
	HttpServletRequest servletRequest;

	 @Path("{id}")
	 @GET
	 @Produces("application/jsonp")
     public AnswerVoList getSubResource( @Context UriInfo uriInfo,@PathParam("Uid") String uId,@QueryParam("callback") String callback) {
		 System.out.println("-------------------------------------------------------");
		 log.info("callback=-====>"+callback);
		 log.debug("hostGroup2Rest=-====>"+this.hostGroup2Rest);
		 log.debug("hostGroup1Rest=-====>"+this.hostGroup1Rest);
		 return this.getList(2);
     }

	@Path("p1")
	@GET
	@Produces("application/json")
	public AnswerVoList getSubResource2(@Context UriInfo uriInfo,
			@PathParam("Uid") String uId) {

		return this.getList(2);
	}
	
	private AnswerVoList getList(int size) {
		AnswerVoList list = new AnswerVoList();
		for (int i = 0; i < size; i++) {
			AnswerVO vo = new AnswerVO();
			vo.setAnswerId(i);
			vo.setAnswerTitle("测试" + i);
			list.getAnswerVoList().add(vo);
		}
		return list;
	}

	private void printMap(MultivaluedMap<String, String> callback) {
		for (Object key : callback.keySet()) {
			System.out.println("key:" + key + "--" + callback.get(key).get(0));
		}
	}
}
